import BaseXform from '../base-xform';
import ColorXform from '../style/color-xform';
import PageSetupPropertiesXform from './page-setup-properties-xform';
import OutlinePropertiesXform from './outline-properties-xform';

class SheetPropertiesXform extends BaseXform {
    constructor() {
        super();

        this.map = {
            tabColor: new ColorXform('tabColor'),
            pageSetUpPr: new PageSetupPropertiesXform(),
            outlinePr: new OutlinePropertiesXform(),
        };
    }

    get tag() {
        return 'sheetPr';
    }

    render(xmlStream, model) {
        if (model) {
            xmlStream.addRollback();
            xmlStream.openNode('sheetPr');

            let inner = false;
            inner = this.map.tabColor.render(xmlStream, model.tabColor) || inner;
            inner = this.map.pageSetUpPr.render(xmlStream, model.pageSetup) || inner;
            inner = this.map.outlinePr.render(xmlStream, model.outlineProperties) || inner;

            if (inner) {
                xmlStream.closeNode();
                xmlStream.commit();
            } else {
                xmlStream.rollback();
            }
        }
    }

    parseXmlObject(node) {
        if (node._name === this.tag) {
            this.reset();
            node._elements.forEach((item) => {
                if (this.map[item._name]) {
                    this.parser = this.map[item._name];
                    this.parser.parseXmlObject(item);
                    this.parser = undefined;
                }
            })
            if (this.map.tabColor.model || this.map.pageSetUpPr.model || this.map.outlinePr.model) {
                this.model = {};
                if (this.map.tabColor.model) {
                    this.model.tabColor = this.map.tabColor.model;
                }
                if (this.map.pageSetUpPr.model) {
                    this.model.pageSetup = this.map.pageSetUpPr.model;
                }
                if (this.map.outlinePr.model) {
                    this.model.outlineProperties = this.map.outlinePr.model;
                }
            } else {
                this.model = null;
            }
            return true;
        }
        return false;
    }

    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        if (node.name === this.tag) {
            this.reset();
            return true;
        }
        if (this.map[node.name]) {
            this.parser = this.map[node.name];
            this.parser.parseOpen(node);
            return true;
        }
        return false;
    }

    parseText(text) {
        if (this.parser) {
            this.parser.parseText(text);
            return true;
        }
        return false;
    }

    parseClose(name) {
        if (this.parser) {
            if (!this.parser.parseClose(name)) {
                this.parser = undefined;
            }
            return true;
        }
        if (this.map.tabColor.model || this.map.pageSetUpPr.model || this.map.outlinePr.model) {
            this.model = {};
            if (this.map.tabColor.model) {
                this.model.tabColor = this.map.tabColor.model;
            }
            if (this.map.pageSetUpPr.model) {
                this.model.pageSetup = this.map.pageSetUpPr.model;
            }
            if (this.map.outlinePr.model) {
                this.model.outlineProperties = this.map.outlinePr.model;
            }
        } else {
            this.model = null;
        }
        return false;
    }
}

export default SheetPropertiesXform;
